package com.example.springbootv1.controller;

import com.example.springbootv1.pojo.LoginLog;
import com.example.springbootv1.service.LoginLogService;
import com.example.springbootv1.util.Result;
import org.apache.ibatis.annotations.Param;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController  // 保证返回的是JSON数据 不能直接用@Controller，或者是在每个请求前面加上@ResponseBody
@RequestMapping("/loginLog")
@CrossOrigin(origins = "*")  // 开放所有域，非GET请求会有安全问题
public class LoginLogController {
    @Autowired
    private LoginLogService loginLogService;

    /**
     * 查询列表
     * @return Result
     */
    @GetMapping("/list")
    public Result getLoginLogList() {
        return Result.success(loginLogService.getLoginLogList());
    }

    /**
     * 从URL中获取参数需要 @PathVariable
     * @param loginLogId
     * @return Result
     */
    @GetMapping("/getById/{loginLogId}")
    public Result getById(@PathVariable Long loginLogId) {
        return Result.success(loginLogService.queryByLoginLogId(loginLogId));
    }

    /**
     * 新增  从前端获取对象，需要@RequestBody
     * @return Result
     */
    @PostMapping("/add")
    public Result add(@RequestBody LoginLog loginLog) {
        return Result.success(loginLogService.insertLoginLog(loginLog));
    }

    /**
     * 更新数据
     * @param loginLog 日志对象
     * @return Result
     */
    @PutMapping("/update")
    public Result update(@RequestBody LoginLog loginLog) {
        return Result.success(loginLogService.updateLoginLog(loginLog));
    }

    /**
     * 删除日志
     * @param loginLogId 日志ID
     * @return Result
     */
    @DeleteMapping("/delete/{loginLogId}")
    public Result delete(@PathVariable Long loginLogId) {
        return Result.success(loginLogService.deleteLoginLogById(loginLogId));
    }

    /**
     * 登录验证    多个参数传递到XML里面的方法
     * @param loginLogName 登录名
     * @param loginLogFlag 登录方法
     * @return 日志对象
     */
    @PostMapping("/login")
    public Result login(@RequestParam(value = "loginLogName", required = false) String loginLogName,
                        @RequestParam(value = "loginLogFlag", required = false) String loginLogFlag) {
        LoginLog loginLog = loginLogService.login(loginLogName, loginLogFlag);
        if (loginLog != null) {
            return Result.success(loginLog);
        } else {
            return Result.error("登录失败！");
        }

    }

    /**
     * 分页查询
     * @param loginLog 日志对象
     * @return Result
     * @serialData 对象列表
     */
    @PostMapping("/fenYeList")
    public Result getLoginLogByFenYe(@RequestBody LoginLog loginLog) {
        return Result.success(loginLogService.getLoginLogByFenYe(loginLog));
    }

    /**
     * 搜索，模糊匹配
     * @param loginLog
     * @return
     * 查询为什么不能添加@RequestBody，我这个没报错
     */
    @GetMapping("/search")
    public Result getLoginLogByLoginLog(LoginLog loginLog) {
        System.out.println("------------"+loginLog+"---------------");
        List<LoginLog> searchLoginLog = loginLogService.getLoginLogByLoginLog(loginLog);
        if (!searchLoginLog.isEmpty()) {
            return Result.success(searchLoginLog);
        } else {
            return Result.error("没查到！");
        }

    }

//    public Result getLoginLogByLoginLog(
//            @RequestParam(required = false) String loginLogName,
////            @RequestParam(required = false) Date loginLogTime,
//            @RequestParam(required = false) Double loginLogLevel,
//            @RequestParam(required = false) String loginLogFlag
//    ) {
//        LoginLog loginLog = new LoginLog();
//        loginLog.setLoginLogName(loginLogName);
////        if (l) {loginLog.setLoginLogTime(loginLogTime);}
//        loginLog.setLoginLogFlag(loginLogFlag);
//        loginLog.setLoginLogLevel(loginLogLevel);
//        System.out.println("------------"+loginLog+"---------------");
//        List<LoginLog> searchLoginLog = loginLogService.getLoginLogByLoginLog(loginLog);
//        if (!searchLoginLog.isEmpty()) {
//            return Result.success(searchLoginLog);
//        } else {
//            return Result.error("没查到！");
//        }
//
//    }

    @DeleteMapping("/deleteBatch/{loginLogIds}")
    public Result deleteLoginLogBatch(@PathVariable List<Long> loginLogIds) {
        int count = loginLogService.deleteLoginLogBatch(loginLogIds);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.error("删除失败！");
        }
    }

    @GetMapping("/getLoginLogBatch/{loginLogIds}")
    public Result getLoginLogBatch(@PathVariable Long[] loginLogIds) {
        return Result.success(loginLogService.getLoginLogBatch(loginLogIds));
    }
}
